//
//  LivePickCoverView.swift
//  HinsLive
//
//  Created by haoxian on 2019/5/5.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit
import RxSwift


class LivePickCoverView: UIView {
    
    let coverImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    let albumBtn     = UIButton.create(with: "相册")
    let takePhotoBtn = UIButton.create(with: "拍照")
    let cancelBtn    = UIButton.create(with: "取消")
    let doneBtn      = UIButton.create(with: "确认")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setPrepareToPick()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(coverImageView)
        coverImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        addSubview(albumBtn)
        albumBtn.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(40)
            make.centerX.equalToSuperview().offset(-80)
            make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom).offset(-20)
        }
        addSubview(takePhotoBtn)
        takePhotoBtn.snp.makeConstraints { (make) in
            make.height.width.equalTo(albumBtn)
            make.centerX.equalToSuperview().offset(80)
            make.bottom.equalTo(albumBtn)
        }
        addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.width.height.centerX.bottom.equalTo(albumBtn)
        }
        addSubview(doneBtn)
        doneBtn.snp.makeConstraints { (make) in
            make.height.width.centerX.bottom.equalTo(takePhotoBtn)
        }
    }
    
    func setPrepareToPick() {
        backgroundColor = .clear
        coverImageView.image = nil
        cancelBtn.isHidden = true
        doneBtn.isHidden = true
        albumBtn.isHidden = false
        takePhotoBtn.isHidden = false
    }
    
    func setDidPicked() {
        backgroundColor = .black
        cancelBtn.isHidden = false
        doneBtn.isHidden = false
        albumBtn.isHidden = true
        takePhotoBtn.isHidden = true
    }
}


private extension UIButton {
    static func create(with title: String) -> UIButton {
        let btn = UIButton(type: .custom)
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .standardColor
        btn.layer.cornerRadius = 4
        return btn
    }
}
